package com.lgm.ht.controller;

import com.lgm.ht.dto.TypeDTO;
import com.lgm.ht.exception.ParamException;
import com.lgm.ht.service.TypeService;
import com.lgm.ht.utils.ResultModel;
import com.lgm.ht.utils.StringUtil;
import com.lgm.ht.vo.TypeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/type")
public class TypeController {
    final private TypeService typeService;

    @GetMapping //http://localhost:8080/type?shopId=2
    public ResultModel<List<TypeVO>> find(TypeDTO typeDTO){
        if(typeDTO.getShopId()==null){
            throw new ParamException("查询分类信息,参数错误");
        }
        return typeService.find(typeDTO);
    }

    @PostMapping
    public ResultModel add(TypeDTO typeDTO){
        if(typeDTO.getShopId()==null || StringUtil.isEmpty(typeDTO.getName())){
            throw new ParamException("查询分类信息,参数错误");
        }
        return typeService.add(typeDTO);
    }

    @PutMapping("/{id}")
    public ResultModel update(@PathVariable("id")int id, TypeDTO typeDTO){
        if(typeDTO.getShopId()==null || StringUtil.isEmpty(typeDTO.getName())){
            throw new ParamException("查询分类信息,参数错误");
        }
        return typeService.update(id,typeDTO);
    }

    @DeleteMapping("/{id}")
    public ResultModel deleteById(@PathVariable("id")int id){
        return typeService.deleteById(id);
    }
}
